﻿/**
 * 主程序，测试虚拟机的运行
 *
 * 创建时间：2022年5月21日
 * 作者：李述铜
 * 联系邮箱: 527676163@qq.com
 */
#include <stdio.h>
#include <stdlib.h>
#include "simu8051.h"
#include "simu8051_tools.h"

// nop, nop, sjmp $
static uint8_t t0_nop_code[] = {0x00, 0x00, 0x80, 0xFE};

void check_result(void) {
}

static const char * file_name[] = {
    "../../test/t1_simple/Objects/code.hex",
    //"asm_test/t2_move_0/Objects/code.hex",
    //"asm_test/t2_move_1/Objects/code.hex",
    //"asm_test/t2_move_2/Objects/code.hex",
    //"asm_test/t2_move_3/Objects/code.hex",
    //"asm_test/t3_movc_0/Objects/code.hex",
    //"asm_test/t5_push_pop/Objects/code.hex",
    //"asm_test/t5_push_pop/Objects/code.hex",
    //"asm_test/t6_xch/Objects/code.hex",
    //"asm_test/t7_anl/Objects/code.hex",
    //"asm_test/t8_orl/Objects/code.hex",
    //"asm_test/t9_xrl/Objects/code.hex",
    //"asm_test/t10_cpl_rr_rl/Objects/code.hex",
    //"asm_test/t11_add_0/Objects/code.hex",         // flag
    //"asm_test/t11_add_1/Objects/code.hex",
    //"asm_test/t12_inc/Objects/code.hex",
    //"asm_test/t13_dec/Objects/code.hex",
    //"asm_test/t14_subb_0/Objects/code.hex",
    //"asm_test/t14_subb_1/Objects/code.hex",
    //"asm_test/t15_mul_div_da/Objects/code.hex",
    //"asm_test/t16_jmp_call_ret/Objects/code.hex",
    //"asm_test/t17_djnz_jz_cjne/Objects/code.hex",
    //"asm_test/t18_bit_jb_jc/Objects/code.hex",
};

void test_instr(void) {
    for (int i = 0; i < sizeof(file_name) / sizeof(const char *); i++) {
        uint16_t pc;
        uint8_t * code;

        printf("begin test: %s\n", file_name[i]);

        code = simu8051_load_hexfile(file_name[i]);
        if (code == NULL) {
            printf("load hex file failed: %s", file_name[i]);
            exit(-1);
        }

        simu8051_reset();
        simu8051_load(code);
        do {
            instr_t instr;

            pc = simu8051_pc();

            simu8051_fetch_instr(&instr);
            simu8051_exec(&instr);
        } while (pc != simu8051_pc());

        check_result();

        free(code);

        printf("test ok.\n");
    }
}

int main() {
    simu8051_init();
    test_instr();
    return 0;
}
